Passed
Push — develop ( ddd51f...56e412 )
by Bjarn
01:22
created

ServiceController   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 113
dl 0
loc 142
rs 9.76
c 0
b 0
f 0
wmc 33
1
import Dnsmasq from '../services/dnsmasq'
2
import Elasticsearch from '../services/elasticsearch'
3
import Mailhog from '../services/mailhog'
4
import Mariadb from '../services/mariadb'
5
import Mysql from '../services/mysql'
6
import Mysql57 from '../services/mysql57'
7
import Mysql80 from '../services/mysql80'
8
import Nginx from '../services/nginx'
9
import PhpFpm from '../services/phpFpm'
10
import PhpFpm72 from '../services/phpFpm72'
11
import PhpFpm73 from '../services/phpFpm73'
12
import PhpFpm74 from '../services/phpFpm74'
13
import PhpFpm80 from '../services/phpFpm80'
14
import Redis from '../services/redis'
15
import Service from '../services/service'
16
import {getLinkedDatabase} from '../utils/database'
17
import {getLinkedPhpVersion} from '../utils/phpFpm'
18
19
class ServiceController {
20
    allServices: Service[] = [
21
        new Dnsmasq(),
22
        new Elasticsearch(),
23
        new Mailhog(),
24
        new Nginx(),
25
        new Mariadb(),
26
        new Mysql80(),
27
        new Mysql57(),
28
        new PhpFpm80(),
29
        new PhpFpm74(),
30
        new PhpFpm73(),
31
        new PhpFpm72(),
32
        new Redis()
33
    ]
34
35
    executeStart = async (serviceName: string | undefined): Promise<boolean> => {
36
        if (!serviceName) {
37
            for (const service of this.allServices) {
38
                try {
39
                    if (service instanceof Mysql) {
40
                        const linkedDatabase = await getLinkedDatabase()
41
                        if (linkedDatabase.service !== service.service)
42
                            continue
43
                    }
44
                    if (service instanceof PhpFpm) {
45
                        const linkedPhpVersion = await getLinkedPhpVersion()
46
                        if (linkedPhpVersion.service !== service.service)
47
                            continue
48
                    }
49
                    console.log(`Starting ${service.service}...`)
50
                    await service.start()
51
                } catch (e) {
52
                    console.log(`Failed to start ${service.service}: ${e.message}`)
53
                }
54
            }
55
            console.log(`Successfully started all Jale services.`)
56
            return true
57
        }
58
59
        for (const service of this.allServices) {
60
            if (service.service === serviceName) {
61
                console.log(`Starting ${service.service}...`)
62
                try {
63
                    await service.start()
64
                    console.log(`Successfully started ${serviceName}.`)
65
                    return true
66
                } catch (e) {
67
                    console.log(`Failed to start ${service.service}: ${e.message}`)
68
                }
69
            }
70
        }
71
72
        console.warn(`Invalid service: ${serviceName}.`)
73
74
        return false
75
    }
76
77
    executeStop = async (serviceName: string | undefined): Promise<boolean> => {
78
        if (!serviceName) {
79
            for (const service of this.allServices) {
80
                try {
81
                    if (service instanceof Mysql) {
82
                        const linkedDatabase = await getLinkedDatabase()
83
                        if (linkedDatabase.service !== service.service)
84
                            continue
85
                    }
86
                    if (service instanceof PhpFpm) {
87
                        const linkedPhpVersion = await getLinkedPhpVersion()
88
                        if (linkedPhpVersion.service !== service.service)
89
                            continue
90
                    }
91
                    console.log(`Stopping ${service.service}...`)
92
                    await service.stop()
93
                } catch (e) {
94
                    console.log(`Failed to stop ${service.service}: ${e.message}`)
95
                }
96
            }
97
98
            console.log(`Successfully stopped all Jale services.`)
99
            return true
100
        }
101
102
        for (const service of this.allServices) {
103
            if (service.service === serviceName) {
104
                console.log(`Stopping ${service.service}...`)
105
                try {
106
                    await service.stop()
107
                    console.log(`Successfully stopped ${serviceName}.`)
108
                    return true
109
                } catch (e) {
110
                    console.log(`Failed to stop ${service.service}: ${e.message}`)
111
                }
112
            }
113
        }
114
115
        console.warn(`Invalid service: ${serviceName}.`)
116
117
        return false
118
    }
119
120
    executeRestart = async (serviceName: string | undefined): Promise<boolean> => {
121
        if (!serviceName) {
122
            for (const service of this.allServices) {
123
                try {
124
                    if (service instanceof Mysql) {
125
                        const linkedDatabase = await getLinkedDatabase()
126
                        if (linkedDatabase.service !== service.service)
127
                            continue
128
                    }
129
                    if (service instanceof PhpFpm) {
130
                        const linkedPhpVersion = await getLinkedPhpVersion()
131
                        if (linkedPhpVersion.service !== service.service)
132
                            continue
133
                    }
134
                    console.log(`Restarting ${service.service}...`)
135
                    await service.restart()
136
                } catch (e) {
137
                    console.log(`Failed to restarted ${service.service}: ${e.message}`)
138
                }
139
            }
140
            console.log(`Successfully restarted all Jale services.`)
141
            return true
142
        }
143
144
        for (const service of this.allServices) {
145
            if (service.service === serviceName) {
146
                console.log(`Restarting ${service.service}...`)
147
                try {
148
                    await service.restart()
149
                    console.log(`Successfully restarted ${serviceName}.`)
150
                    return true
151
                } catch (e) {
152
                    console.log(`Failed to restarted ${service.service}: ${e.message}`)
153
                }
154
            }
155
        }
156
157
        console.warn(`Invalid service: ${serviceName}.`)
158
159
        return false
160
    }
161
162
}
163
164
export default ServiceController